﻿using System;
using System.Configuration;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Data.SQLite;
using System.Data;


using CqusInfo.RAL.User;
using CqusInfo.CR.SqlHelper;

namespace CqusInfo.DAL.User
{
    public class User_InfoToBasicDAL:Interface.IUser_InfoToBasicDAL
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public User_InfoToBasicDAL()
        {

        }

        public List<User_InfoToBasicModel> Query_ByUserName(string userName)
        {
            List<User_InfoToBasicModel> userList = new List<User_InfoToBasicModel>();
            DataSet ds = SQLiteHelper.Query("tb_User_InfoToBasic", "c_vc_uitbId", userName);
            string cacheId=string.Format("User_InfoToBasic_{0}",userName);
            userList = SqlData.GetQueryList<User_InfoToBasicModel>(cacheId, "User_InfoToBasicModel", ds);

            return userList;
        }

        /// <summary>
        /// 得到数据参数
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <param name="model"></param>
        /// <returns>parameters</returns>
        private SQLiteParameter[] GetParameter(string userName, User_InfoToBasicModel model)
        {
            SQLiteParameter[] parameters = { 
                                       new SQLiteParameter("@userName",DbType.String),
                                       new SQLiteParameter("@mobile",DbType.String),
                                       new SQLiteParameter("@qq",DbType.String),
                                       new SQLiteParameter("@address",DbType.String),
                                       new SQLiteParameter("@sex",DbType.String),
                                       new SQLiteParameter("@age",DbType.Int32,4),
                                       new SQLiteParameter("@photo",DbType.String),
                                       new SQLiteParameter("@realName",DbType.String),
                                       new SQLiteParameter("@constellation",DbType.String),
                                       new SQLiteParameter("@bloodType",DbType.String),
                                       new SQLiteParameter("@province",DbType.String),
                                       new SQLiteParameter("@city",DbType.String),
                                       new SQLiteParameter("@home",DbType.String)
                                       };
            parameters[0].Value = userName;
            parameters[1].Value = model.c_vc_mobile;
            parameters[2].Value = model.c_vc_qq;
            parameters[3].Value = model.c_vc_address;
            parameters[4].Value = model.c_vc_sex;
            parameters[5].Value = model.c_i_age;
            parameters[6].Value = model.c_vc_photo;
            parameters[7].Value = model.c_vc_realName;
            parameters[8].Value = model.c_vc_constellation;
            parameters[9].Value = model.c_vc_bloodType;
            parameters[10].Value = model.c_vc_province;
            parameters[11].Value = model.c_vc_city;
            parameters[12].Value = model.c_vc_home;

            return parameters;
        }

        public Int32 Add(string userName, User_InfoToBasicModel model)
        {
            StringBuilder sqlStr = new StringBuilder();

            sqlStr.Append("insert into tb_User_InfoToBasic values(");
            sqlStr.Append("c_vc_uitbId=@userName,c_vc_mobile=@moblie,c_vc_qq=@qq,c_vc_address=@address,c_vc_sex=@sex,c_i_age=@age,c_vc_photo=@photo,c_vc_realName=@realName,c_vc_constellation=@constellation,c_vc_bloodType=@bloodType,c_vc_province=@province,c_vc_city=@city,c_vc_home=@home)");
            SQLiteParameter[] parameters = GetParameter(userName, model);

            return SQLiteHelper.ExecuteNonQuery(sqlStr.ToString(), parameters);
        }

        public Int32 Update(string userName, User_InfoToBasicModel model)
        {
            StringBuilder sqlStr = new StringBuilder();

            sqlStr.Append("update tb_User_InfoToBasic set ");
            sqlStr.Append("c_vc_mobile=@moblie,c_vc_qq=@qq,c_vc_address=@address,c_vc_sex=@sex,c_i_age=@age,c_vc_photo=@photo,c_vc_realName=@realName,c_vc_constellation=@constellation,c_vc_bloodType=@bloodType,c_vc_province=@province,c_vc_city=@city,c_vc_home=@home where c_vc_uitbId=@userName)");
            SQLiteParameter[] parameters = GetParameter(userName, model);

            return SQLiteHelper.ExecuteNonQuery(sqlStr.ToString(), parameters);
        }

        public Int32 Delete(string userName)
        {
            return SQLiteHelper.Delete("tb_User_InfoToBasic", "c_vc_uitbId", userName);
        }

    }
}
